package taskmaster.util;

import taskmaster.auth.User;

import java.sql.*;

/**
 * taskmaster.util.RunStatement
 * <BR><BR>
 * Abstract class that provides two methods:
 * <UL>
 * <LI>running a generic sql query and returning its ResultSet
 * <LI>getting a new PreparedStatement for this User based on the sqlStatement
 * </UL>
 */
public abstract class RunStatement
{

    /**
     * run a generic sql query and return its ResultSet
     */
    public ResultSet runStatement(User user, String sqlStatement) throws SQLException
    {
        Connection c = user.getConnection();
        if ((c == null) || (c.isClosed()))
        {
            throw new SQLException("invalid user connection");
        }
        Statement stmt = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = stmt.executeQuery(sqlStatement);
        return rs;
    }

    /**
     * get a new PreparedStatement for this User based on the sqlStatement
     */
    public PreparedStatement getPreparedStatement(User user, String sqlStatement) throws SQLException
    {
        Connection c = user.getConnection();
        if ((c == null) || (c.isClosed()))
        {
            throw new SQLException("invalid user connection");
        }
        PreparedStatement pstmt = c.prepareStatement(sqlStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        return pstmt;
    }

}
